package com.qs.education.bean.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.qs.education.bean.dao.IEducationSubjectUnitDao;
import com.qs.education.bean.entity.EducationSubjectUnit;

/**
 * @class EducationSubjectUnitDaoImpl
 * @author Leon
 * @description
 * @date Nov 19, 2018
 */

@Repository
public class EducationSubjectUnitDaoImpl extends BaseDaoImpl<EducationSubjectUnit> implements IEducationSubjectUnitDao {

	@Override
	public List<EducationSubjectUnit> findSubjectUnitByGradeAndSubject(EducationSubjectUnit subjectUnit) {
		Map<String, Object> params = new HashMap<String, Object>();
		String jql = "Select ea from EducationSubjectUnit ea where 1=1 ";
		if (subjectUnit.getId() != null) {
			params.put("id", subjectUnit.getId());
			jql = jql +"and ea.id = :id ";
		}
		if (subjectUnit.getTitle() != null) {
			params.put("title", "%" + subjectUnit.getTitle() + "%");
			jql = jql +"and ea.title like :title ";
		}
		if (subjectUnit.getGrade()!=null) {
			if(subjectUnit.getGrade().getId()!=null) {
				params.put("gradeId", subjectUnit.getGrade().getId());
				jql = jql +"and ea.grade.id = :gradeId ";
			}
		}
		if (subjectUnit.getSubject()!=null) {
			if(subjectUnit.getSubject().getId()!=null) {
				params.put("subjectId", subjectUnit.getSubject().getId());
				jql = jql +"and ea.subject.id = :subjectId ";
			}
		}
		System.out.println(jql);
		List<EducationSubjectUnit> list = query(jql, EducationSubjectUnit.class, params);
		return list;
	}
}
